package com.example.hello.hello.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.hello.hello.AppApplication;
import com.example.hello.hello.R;
import com.example.hello.hello.api.remote.OSAppApi;
import com.example.hello.hello.base.BaseActivity;
import com.example.hello.hello.bean.User;
import com.example.hello.hello.bean.UserMess;
import com.example.hello.hello.bean.UserMessInfo;
import com.example.hello.hello.utils.MD5Utils;
import com.example.hello.hello.utils.StringUtils;
import com.example.hello.hello.utils.VerificationUtils;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;


/**
 * A login screen that offers login via mobile/password.
 */
public class LoginActivity extends BaseActivity  implements View.OnClickListener{

    private Button login;
    private EditText password,usernmae;
    private ImageButton ib_username_delete,ib_password_delete;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initTitleBar();
        setMidTxt("登录");
        setRightImg(getResources().getDrawable(R.mipmap.reg_img));
        login = (Button) this.findViewById(R.id.main_btn_login);
        usernmae = (EditText) this.findViewById(R.id.et_usernmae);
        password = (EditText)this.findViewById(R.id.et_password);
        ib_username_delete = (ImageButton) this.findViewById(R.id.ib_username_delete);
        ib_password_delete = (ImageButton) this.findViewById(R.id.ib_password_delete);
        login.setOnClickListener(this);
        ib_username_delete.setOnClickListener(this);
        ib_password_delete.setOnClickListener(this);

        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!s.equals(null)){
                    ib_password_delete.setVisibility(View.VISIBLE);
                }else {
                    ib_password_delete.setVisibility(View.GONE);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        usernmae.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!s.equals(null)){
                    ib_username_delete.setVisibility(View.VISIBLE);
                }else {
                    ib_username_delete.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_btn_login:
                Login();
                break;
            case R.id.ib_username_delete:
                usernmae.setText("");
                ib_username_delete.setVisibility(View.GONE);
                break;
            case R.id.ib_password_delete:
                password.setText("");
                ib_password_delete.setVisibility(View.GONE);
                break;
            case R.id.ib_titleback:
                onClickLeftBtn();
                break;
            case R.id.ib_mytitle:
                onClickReghtBtn();
                break;

        }
    }

    @Override
    protected void onClickLeftBtn() {
        handSucess();
    }

    @Override
    protected void onClickReghtBtn() {
        Intent intent = new Intent(LoginActivity.this,RegActivity.class);
        startActivity(intent);
        this.finish();
    }
    public void Login(){
        String usernameStr = usernmae.getText().toString().trim();
        String passwordStr = password.getText().toString().trim();
        if(StringUtils.isEmpty(usernameStr)){
            showToast("请输入手机号");
            usernmae.findFocus();
            return;
        }
        if(!VerificationUtils.isMobile(usernameStr)){
            showToast("请输入正确的手机号");
            usernmae.findFocus();
            return;
        }
        if(StringUtils.isEmpty(passwordStr)){
            showToast("请输入密码");
            usernmae.findFocus();
            return;
        }
        showWaitDialog();
        OSAppApi.login(usernameStr, MD5Utils.MD5(passwordStr),mHandel);
    }

   private final AsyncHttpResponseHandler mHandel = new AsyncHttpResponseHandler() {
       @Override
       public void onSuccess(int i, Header[] headers, byte[] bytes) {
           hideWaitDialog();
           String json = new String(bytes);
           Gson gson = new Gson();
           try {
               UserMess UserMess = gson.fromJson(json, UserMess.class);
               User user = UserMess.getResult();
               AppApplication.getInstance().saveUserInfo(user);
               showToast("登录成功！");
               handSucess();
           } catch (Exception e) {
               try {
                   UserMessInfo UserMessInfo = gson.fromJson(json, UserMessInfo.class);
                   showToast(UserMessInfo.getResult());
               }catch (Exception e1) {
                   showToast("服务器异常");
               }

           }
       }

       @Override
       public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
           hideWaitDialog();
           showToast("获取失败");
       }
   };

    private void handSucess(){
        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
        startActivity(intent);
        this.finish();
    }
}

